package com.beta.restapi.controller;

import com.beta.common.web.ContentType;
import com.beta.dto.user.UserApplyDto;
import com.beta.restapi.internal.Consts;
import com.beta.restapi.internal.annotation.AuthSetting;
import com.beta.entity.Users;
import com.beta.restapi.internal.annotation.AuthStrategy;
import com.beta.restapi.internal.auth.AuthService;
import com.beta.restapi.request.user.UserApplyRequest;
import com.beta.restapi.request.user.UserRegisterRequest;
import com.beta.restapi.request.user.UserResetPasswordRequest;
import com.beta.service.IUsersService;
import com.beta.vo.user.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import ma.glasnost.orika.MapperFacade;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = {Consts.Resource.USER})
@RestController
@RequestMapping(Consts.API_PATH_PREFIX + Consts.Resource.USER)
public class UserController extends BaseController {

    @Autowired
    private IUsersService userService;
    @Autowired
    private AuthService authService;
    @Autowired
    private MapperFacade beanMapper;

    @ApiResponses({
            @ApiResponse(code = HttpStatus.SC_OK, message = "success", response = UserVO.class),
    })
    @GetMapping(value = "", produces = {ContentType.JSON})
    public Object getCurrentUser(@RequestHeader(Consts.HEADER_AUTH_KEY) String token) {
        Users currentUser = authService.getCurrentUser(token);
        UserVO userVO = beanMapper.map(currentUser, UserVO.class);
        return ok(userVO);
    }

    @AuthSetting(level = AuthStrategy.ANONYMOUS)
    @PostMapping(value = "", consumes = {ContentType.JSON}, produces = {ContentType.JSON})
    public Object register(@Valid @RequestBody UserRegisterRequest request) {
        Users users = userService.executeCreateUser(request.getConfirmationCode(), request.getRegisterDto());
        return ok(users);
    }

    @AuthSetting(level = AuthStrategy.ANONYMOUS)
    @PostMapping(value = "/reset_password", produces = {ContentType.JSON})
    public Object resetPassword(@RequestBody UserResetPasswordRequest requestBody) {
        userService.executeResetPassword(requestBody.getConfirmationCode(), requestBody.getResetPasswordDto());
        return ok();
    }

    @ApiOperation(value = "/apply", notes = "用户（小B）审核接口")
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.PATCH}, value = "/apply", consumes = {ContentType.JSON}, produces = {ContentType.JSON})
    public Object apply(@RequestHeader(Consts.HEADER_AUTH_KEY) String token,
                        @RequestBody UserApplyRequest applyRequest) {
        UserApplyDto applyInfo = applyRequest.getApplyInfo();
        applyInfo.setId(getCurrentUserId());
        userService.executeApplyUser(applyInfo);
        authService.updateCurrentUser(request);
        return getCurrentUser(token);
    }

}
